package com.sample.navigation.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class ScreenMetricUtil {
    private static int dpWidth;
    private static int dpHeight;
    private static int width;
    private static int height;
    private static int fullWidth;
    private static int fullHeight;
    private static float density;
    private static int densityDpi;
    private static int barHeight;
    private static int navHeight;
    private static Context mContext;

    public static void initialize(Context context){
        mContext = context;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;         // 屏幕宽度（像素）
        height = dm.heightPixels;       // 屏幕高度（像素）
        density = dm.density;           // 屏幕密度（0.75 / 1.0 / 1.5）
        densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        dpWidth = (int) (width / density);  // 屏幕宽度(dp)
        dpHeight = (int) (height / density);// 屏幕高度(dp)

        getStatusBarHeight();
        getNavgationHeight();
    }

    public static int getDpWidth(){return dpWidth;}

    public static int getDpHeight(){return dpHeight;}

    public static int getWidth(){return width;}

    public static int getHeight(){return height;}

    public static int getDpFullWidth(){return (int) (getFullWidth() / density) + 1;}

    public static int getDpFullHeight(){return (int) (getFullHeight() / density) + 1;}

    public static int getFullWidth(){return width;}

    public static int getFullHeight(){return height+navHeight;}

    public static float getDensity() { return density; }

    public static int getBarHeight() {
        return barHeight;
    }

    public static int getNavHeight() {
        return navHeight;
    }

    public static int getDensityDpi() {
        return densityDpi;
    }

    private static void  getStatusBarHeight() {
        int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen",
                "android");
        if (resourceId !=0) {
            barHeight = mContext.getResources().getDimensionPixelSize(resourceId);
        }
    }

    private static void getNavgationHeight() {
        int rid = mContext.getResources().getIdentifier("config_showNavigationBar", "bool", "android");
        if (rid!=0){
            int resourceId = mContext.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            navHeight = mContext.getResources().getDimensionPixelSize(resourceId);
        }
    }
}
